/*
	       File: ucService.cs
	Create Date: 2007-08-16
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public partial class ucService : UserControl
    {
        #region Private member fields
        ConfigurationServices _ServiceConfiguration;
        #endregion

        #region Constructors and cleanup methods
        public ucService()
        {
            InitializeComponent();
            _ServiceConfiguration = null;
        }
        #endregion

        #region Private implementation methods
        public void OnUpdateServiceSettings()
        {
            if (null != UpdateServiceSettings)
            {
                UpdateServiceSettings(_ServiceConfiguration);
            }
        }
        private int ConvertConfigValues(string value, int defaultValue)
        {
            if (null == value)
            {
                return defaultValue;
            }

            int outValue = defaultValue;
            if (false == int.TryParse(value, out outValue))
            {
                return defaultValue;
            }

            return outValue;
        }
        #endregion

        #region Class properties
        #endregion

        #region Events handlers
        private void btnArcDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (DialogResult.OK == fbd.ShowDialog())
            {
                txtArchiveDir.Text = fbd.SelectedPath;
            }
        }
        private void btnAsmDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (DialogResult.OK == fbd.ShowDialog())
            {
                txtAsmDir.Text = fbd.SelectedPath;
            }
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            _ServiceConfiguration.ArchiveDirectory = txtArchiveDir.Text;
            _ServiceConfiguration.AssemblyDirectory = txtAsmDir.Text;
            _ServiceConfiguration.MaxDirectoryWorkerThreads = ((int)numMaxDirThreads.Value).ToString();
            _ServiceConfiguration.MaxFileProcessorWorkThreads = ((int)numMaxFileThreads.Value).ToString();
            _ServiceConfiguration.PollingTime = ((int)numPollingTime.Value).ToString();

            OnUpdateServiceSettings();
        }
        #endregion

        #region Public interface
        public void LoadServiceConfiguration(ConfigurationServices services)
        {
            if (null != services)
            {
                _ServiceConfiguration = services;
                int maxDirThreads = ConvertConfigValues(_ServiceConfiguration.MaxDirectoryWorkerThreads, 3);
                int maxFilethreads = ConvertConfigValues(_ServiceConfiguration.MaxFileProcessorWorkThreads, 3);
                int pollingTime = ConvertConfigValues(_ServiceConfiguration.PollingTime, 30);

                numMaxDirThreads.Value = maxDirThreads;
                numMaxFileThreads.Value = maxFilethreads;
                numPollingTime.Value = pollingTime;
                txtArchiveDir.Text = _ServiceConfiguration.ArchiveDirectory;
                txtAsmDir.Text = _ServiceConfiguration.AssemblyDirectory;
            }
        }
        public delegate void UpdateServiceSettingsDelegate(ConfigurationServices services);
        public event UpdateServiceSettingsDelegate UpdateServiceSettings;
        #endregion
    }
}
